/*
 * Created by IntelliJ IDEA.
 * User: amrk
 * Date: 16/03/2004
 * Time: 21:00:20
 */
package com.talios.jira.browser;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.IconLoader;
import com.talios.jira.JiraException;
import com.talios.jira.idea.JiraConfigurationComponent;
import com.talios.jira.rpc.JiraRpcClient;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class IssueResolution {
    private static final Logger LOGGER = Logger.getInstance("Jira Browser");

    private JPanel mainPanel;
    private JButton cancel;
    private JButton resolve;
    private JTextArea comment;
    private JComboBox fixfor;
    private JComboBox resolution;

    private JFrame dialog;
    private Project project;
    private JiraItem item;
    private JLabel description;
    private JiraListComboBoxModel unleasedVersionsComboBoxModel;
    private JiraListComboBoxModel jiraListComboBoxModel;

    public IssueResolution(final Project project) {
        this.project = project;

        dialog = new JFrame();
        dialog.setBounds(500, 500, 500, 250);
        dialog.getContentPane().add(mainPanel);
        resolve.setIcon(IconLoader.getIcon("/actions/sync.png"));
        cancel.setIcon(IconLoader.getIcon("/actions/cancel.png"));

        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
            }
        });

        resolve.addActionListener(/**
         * @jira INTPLUG-75 When an issue being worked on is "resolved", the plugin should stop working on it.
         */
                new ActionListener() {
                    /**
                     * @jira INTPLUG-75 When an issue being worked on is "resolved", the plugin should stop working on it.
                     */
                    public void actionPerformed(ActionEvent e) {

                        Messages.showErrorDialog("Jira", "Sorry, untill I switch to using SOAP this is no longer workin :(");

/*
                        try {
                            if (item.isBeingWorkedOn()) {
                                IssueManager.getInstance().stopWorkingOnIssues(project);
                            }


                            JiraRpcClient rpc = new JiraRpcClient(item.getServer());

                            String statusId = null;
                            List statusList = rpc.getStatuses();
                            for (Iterator iterator = statusList.iterator(); iterator.hasNext();) {
                                Hashtable hashtable = (Hashtable) iterator.next();
                                if ("Resolved".equals(hashtable.get("name"))) {
                                    statusId = (String) hashtable.get("id");
                                }
                            }

                            Vector fields = new Vector();

                            Hashtable field = new Hashtable();
                            field.put("id", "fixVersions");
                            field.put("values", new String[] {unleasedVersionsComboBoxModel.getIdOfSelectedItem()});
                            fields.add(field);

                            field = new Hashtable();
                            
                            field.put("id", "status");
                            field.put("values", statusId);
                            fields.add(field);


                            field = new Hashtable();
                            field.put("id", "resolution");
                            field.put("values", jiraListComboBoxModel.getIdOfSelectedItem());
                            fields.add(field);

                            int workFlowActionId = 2; // resolve
                            rpc.progressWorkflowAction(item.getKey(), workFlowActionId, fields);

                            JiraConfigurationComponent config = project.getComponent(JiraConfigurationComponent.class);
                            config.refreshIssues();

                            dialog.setVisible(false);
                        } catch (JiraException ex) {
                            LOGGER.error("Unable to resolve issue", ex);
                        }
*/
                    }
                });
    }

    public void resolveIssue(JiraItem item) {
        this.item = item;

        dialog.setTitle("Resolving issue: " + item.getKey());
        description.setText(item.getTitle());

        jiraListComboBoxModel = item.getProject().getServer().getIssueResolutionsModel();
        resolution.setModel(jiraListComboBoxModel);
        unleasedVersionsComboBoxModel = item.getProject().getUnreleasedVersionsModel();
        unleasedVersionsComboBoxModel.reverseItems();
        fixfor.setModel(unleasedVersionsComboBoxModel);

        resolution.setSelectedIndex(0);
        fixfor.setSelectedIndex(unleasedVersionsComboBoxModel.getSize() - 1);

        dialog.setVisible(true);
    }


}